/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpaintball;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;

/**
 *
 * @author ondra
 */
public class Canvas extends JPanel {

    private BufferedImage bi;
    private Image groundImg;
    private Graphics2D g2d;
    private Dimension dim;

    Canvas() {
        super();

        // set panel size
        dim = new Dimension(800,500);
        setPreferredSize(dim);
        setSize(dim);
        setFocusable(true);

        // load game sprites
        ImageManager.loadImages();

        // initialize buffered image
        bi = new BufferedImage((int) dim.getWidth(), (int) dim.getHeight(), BufferedImage.TYPE_INT_RGB);
        g2d = bi.createGraphics();

        // load ground image
        groundImg = ImageManager.getImage(ImageManager.IMG_FIELD);

        drawSplashScreen();

    }

    public void paint(Graphics _g) {

        // draw buffered image
        _g.drawImage(bi, 0, 0, null);
    }

    public void drawSplashScreen() {
        g2d.drawString("Welcome to JPaintball :)", 10, 20);
    }

    public void drawObjects(ArrayList objects) {
        // re-create Graphics
        g2d = bi.createGraphics();
        // paint field ground
        g2d.drawImage(groundImg, 0, 0, null);

        // Draw objects
        Iterator<GameObject> it = objects.iterator();
        while (it.hasNext()) {
            it.next().draw(g2d);
        }
        repaint();
    }

}
